<?php
$projectDetails = $projectDetails->row();
$reviewDetails = $reviewDetails->row();

?>
<div class="modal-dialog">
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3><?php echo $this->lang->line('Feedback for Provider');?> - <?php echo $providerDetails->user_name;?></h3>
		</div>
		<form action="" method="POST" id="frmFeedbackProvider">
		<div class="modal-body">
			<div class="col-md-12">
				<?php 
				if(is_object($reviewDetails)) {
					$provider = getUserInfo($reviewDetails->provider_id);
				?>
				<div class="row">
					<div class="control-group">
						<label class="control-label col-md-4"><?php echo $this->lang->line('Provider');?></label>
						<div class="controls col-md-8">
							<a href="<?php echo site_url('programmer/viewProfile/'.$loggedInUser->id);?>"><?php echo $loggedInUser->user_name; ?></a>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="control-group">
						<label class="control-label col-md-4"><?php echo $this->lang->line('Project Name');?></label>
						<div class="controls col-md-8">
							<a href="<?php echo site_url('project/view/'.$reviewDetails->projectid);?>"><?php echo $reviewDetails->project_name; ?></a>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="control-group">
						<label class="control-label col-md-4"><?php echo $this->lang->line('Project Date');?></label>
						<div class="controls col-md-8">
							<?php echo get_date($reviewDetails->created); ?>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="control-group">
						<label class="control-label col-md-4"><?php echo $this->lang->line('Rating');?></label>
						<div class="controls col-md-8">
							<span><input id="preview-user" value="<?php echo $reviewDetails->rating?>" type="number" data-star-captions="{}" data-default-caption="{rating}" class="rating" min=0 max=5  data-size="xs" disabled="disabled" ></span>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="control-group">
						<label class="control-label col-md-4"><?php echo $this->lang->line('comments');?></label>
						<div class="controls col-md-8">
							<?php echo $reviewDetails->comments?>
						</div>
					</div>
				</div>
				<?php }else{ ?>
					<div class="row">
						<div class="control-group">
							<div class="controls col-md-12">
							<p><?php echo $this->lang->line('How would you rate the Provider');?> <strong><?php echo $providerDetails->user_name;?></strong> <?php echo $this->lang->line('for the project');?> <strong><?php echo $projectDetails->project_name;?></strong></p>
                            </div>
						</div>
					</div>
					<div class="row">
						<div class="control-group form-group">
							<div class="controls col-md-12">
								<span><input id="preview-user" name="rate" value="1" type="number" data-star-captions="{}" data-default-caption="{rating}" class="rating" min=0 max=5 data-size="xs" ></span>
							</div>
						</div>
					</div>
					<div class="row">
						<div class="control-group form-group">
							<div class="controls col-md-12">
								<input type="hidden" value="<?php echo $projectDetails->id;?>" id="projectid" name="projectid"/>
	                            <input type="hidden" value="<?php echo $projectDetails->programmer_id;?>" id="providerid" name="providerid"/>
	                            <textarea class="form-control" name="comment" rows="5" placeholder="<?php echo $this->lang->line('Comment');?>"></textarea>
							</div>
						</div>
					</div>
					<p class="loading"></p>
				<?php } ?>
			</div>
			<div style="clear:both"></div>
		</div>
		<div class="modal-footer">
			<?php if(!is_object($reviewDetails)) { ?>
			<button type="submit" name="reviewProgrammer" value="1" class="btn btn-primary">Submit</button>
			<?php }?>
			<button type="button" data-dismiss="modal" class="btn btn-default">Close</button>
		</div>
		</form>
	</div>
</div>

<script>
    $(document).ready(function(){
        $(".rating").rating('refresh',{showCaption:true,clearCaption:'0'});
        var errorHandler = $('.errorHansHandler');
        $("#frmFeedbackProvider").validate({
        	errorElement: "span", // contain the error msg in a span tag
            errorClass: 'help-block',
            errorPlacement: function (error, element) { // render error placement for each input type
                if (element.attr("type") == "radio" || element.attr("type") == "checkbox") { // for chosen elements, need to insert the error after the chosen container
                    error.insertAfter($(element).closest('.form-group').children('div').children().last());
                } else if (element.attr("name") == "dd" || element.attr("name") == "mm" || element.attr("name") == "yyyy") {
                    error.insertAfter($(element).closest('.form-group').children('div'));
                } else {
                    error.insertAfter(element);
                    // for other inputs, just perform default behavior
                }
            },
            highlight: function (element) {
                $(element).closest('.help-block').removeClass('valid');
                // display OK icon
                $(element).closest('.form-group').removeClass('has-success').addClass('has-error').find('.symbol').removeClass('ok').addClass('required');
                // add the Bootstrap error class to the control group
            },
            unhighlight: function (element) { // revert the change done by hightlight
                $(element).closest('.form-group').removeClass('has-error');
                // set error class to the control group
            },
            
            rules:{
                "rate" : {
                    required : true,
                    min: 0.5,
                    max: 5
                },
                "comment" : {
                    required : true,
                },
            },
            messages:{
                "rate":{
                    required : "<?php echo $this->lang->line('rating_valid')?>",
                    min : "<?php echo $this->lang->line('rating_valid')?>",
                    max : "<?php echo $this->lang->line('rating_valid')?>",
                },
                "comment":{
                    required : "<?php echo $this->lang->line('comment_valid')?>",
                },
            },
            submitHandler: function (form) {
                //successHandler.show();
                //errorHandler.hide();
                // submit form
                //$('#form').submit();
                
                jQuery.ajax({
         			type: "POST",
         			data: $("#frmFeedbackProvider").serialize(),
	     		    url: BASE_URL + '/index.php/buyer/reviewProgrammer',
	     		    beforeSend: function() {
	     		    	$('.loading').css("text-align", "center").html('<img src="<?php echo image_url("loading.gif")?>"/>');
	     		    },
	     		    success: function(data) {
	     		    	$('.loading').empty();
	     		    	
	     		    	var res = $.parseJSON(data);
	     		    	if(res.status == "COMPLETED"){
	     		    		$('.loading').html("<?php echo $this->lang->line('review_success')?>");
	     		    		setTimeout(5000);
	     		    		window.location.reload();
		     		    }
	     		    
	     		    }
                });
                
                return false;
            }


        });

    });

</script>
